package com.android.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Enumeration;

/**
 * Created by TenFace
 * on 2017/10/11.
 */

public class AppNecessaryUtils {


    /**
     * 检查网络连接状态
     *
     * @param context context
     * @return true 联网状态， false 未联网状态
     */
    public static boolean checkNetConnectionState(Context context) {
        boolean flag = false;
        //得到网络连接信息
        ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        //去进行判断网络是否连接
        if (manager.getActiveNetworkInfo() != null) {
            flag = manager.getActiveNetworkInfo().isAvailable();
        }
        return flag;
    }


    /**
     * 获取本地ip地址
     *
     * @return ip v4 地址
     */
    public static String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intF = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddress = intF
                        .getInetAddresses(); enumIpAddress.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddress.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取手机唯一标识
     *
     * @param context context
     * @return 手机唯一标识
     */
    @SuppressLint("HardwareIds")
    public static String getUUId(Context context) {
        String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        String uuId = androidID + Build.SERIAL;
        if (TextUtils.isEmpty(uuId)) {
            try {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                uuId = tm.getDeviceId();
            } catch (Exception e) {
                uuId = "DEFAULT_VALUE";
            }
        }
        try {
            return toMD5(uuId);
        } catch (NoSuchAlgorithmException e) {
            return uuId;
        }
    }

    /**
     * 将 唯一标识转换为MD5
     *
     * @param text 内容
     * @return md5值
     * @throws NoSuchAlgorithmException 异常
     */
    private static String toMD5(String text) throws NoSuchAlgorithmException {

        //获取摘要器 MessageDigest
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        //通过摘要器对字符串的二进制字节数组进行hash计算
        byte[] digest = messageDigest.digest(text.getBytes());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            //循环每个字符 将计算结果转化为正整数;
            int digestInt = digest[i] & 0xff;
            //将10进制转化为较短的16进制
            String hexString = Integer.toHexString(digestInt);
            //转化结果如果是个位数会省略0,因此判断并补0
            if (hexString.length() < 2) {
                sb.append(0);
            }
            //将循环结果添加到缓冲区
            sb.append(hexString);
        }
        //返回整个结果
        return sb.toString();
    }

}
